package com.zhou.demo01;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class SaleTicketDemo02 {
    public static void main(String[] args) {
        Ticket2 ticket2 = new Ticket2();

        new Thread(()->{ for (int i = 0; i < 40; i++) ticket2.sale(); }, "A").start();
        new Thread(()->{ for (int i = 0; i < 40; i++) ticket2.sale(); }, "B").start();
        new Thread(()->{ for (int i = 0; i < 40; i++) ticket2.sale(); }, "C").start();
    }
}
class Ticket2{
    private int count = 50;

    Lock lock = new ReentrantLock();

    public void sale(){
        lock.lock();


        try{
            if(count > 0){
                System.out.println(Thread.currentThread().getName()+"卖出了"+(count--)+"票,剩余:"+ count);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            lock.unlock();
        }

    }
}
